package com.zsule.controllers.front;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zsule.constants.CoreConstant;
import com.zsule.controllers.BaseController;
import com.zsule.entity.EUser;
import com.zsule.entity.ShopCert;
import com.zsule.entity.SmokeCert;
import com.zsule.service.IShopCertService;
import com.zsule.service.ISmokeCertService;

@Controller
@RequestMapping("/eshop")
public class CertController extends BaseController {

	@Autowired
	private ISmokeCertService smokeCertService;
	@Autowired
	private IShopCertService shopCertService;

	@RequestMapping(value = "/shopCert", method = RequestMethod.GET)
	public ModelAndView to_shopCert(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/shop/shop_cert.httl");
		EUser user = (EUser) this.getSessionAttribute(request,CoreConstant.USER_SESSION_NAME);
		ShopCert cert = shopCertService.findByUserId(user.getId());
		mav.addObject("cert", cert);
		mav.addObject("user", user);
		mav.addObject("leaf", "shopCert");
		return mav;
	}

	@RequestMapping(value = "/shopCert", method = RequestMethod.POST)
	public ModelAndView shopCert(ShopCert cert,HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("redirect:/eshop/shopCert");
		EUser user = (EUser) this.getSessionAttribute(request,CoreConstant.USER_SESSION_NAME);
		cert.setUserId(user.getId());
		cert.setStatus(SmokeCert.START);
		shopCertService.save(cert);
		return mav;
	}
	@RequestMapping(value = "/smokeCert", method = RequestMethod.GET)
	public ModelAndView to_smokeCert(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/shop/smoke_cert.httl");
		EUser user = (EUser) this.getSessionAttribute(request,CoreConstant.USER_SESSION_NAME);
		SmokeCert smokeCert = smokeCertService.findByUserId(user.getId());
		mav.addObject("cert", smokeCert);
		mav.addObject("user", user);
		mav.addObject("leaf", "smokeCert");
		return mav;
	}
	
	@RequestMapping(value = "/smokeCert", method = RequestMethod.POST)
	public ModelAndView smokeCert(@RequestParam("url") String url,HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("redirect:/eshop/smokeCert");
		EUser user = (EUser) this.getSessionAttribute(request,CoreConstant.USER_SESSION_NAME);
		SmokeCert smokeCert = new SmokeCert();
		smokeCert.setUserId(user.getId());
		smokeCert.setPath(url);
		smokeCert.setStatus(SmokeCert.START);
		smokeCertService.save(smokeCert);
		return mav;
	}

}
